package com.feitubc.tools.fileimport.strategy;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.feitubc.tools.fileimport.IProcessor;
import com.feitubc.tools.fileimport.IProcessorLog;
import com.feitubc.tools.fileimport.StrategyContext;
import com.feitubc.tools.fileimport.domain.ImportLogObject;
import com.feitubc.tools.fileimport.domain.ProcessorMethodEnum;

/**
 * 说明：导入文件基础实现服务
 * @author zhc
 */
@Service("baseImportFileExecutor")
public class BaseImportFileExecutor<T extends ImportLogObject> extends AbstractImportFileTemplate<T> implements IProcessorLog {
	
	@Resource(name = "processor")
	private IProcessor iProcessor;

	@Override
	protected boolean preDataFromFileDataMap(StrategyContext strategyContext) {
		return ProcessorProxyFactory.doExecute(strategyContext, this.iProcessor, ProcessorMethodEnum.preData);
	}

	@Override
	protected boolean preExecuteData(StrategyContext strategyContext) {
		return this.iProcessor.doExecute(strategyContext, null, ProcessorMethodEnum.preExecuteData);
	}

	@Override
	protected boolean executeImport(StrategyContext strategyContext) {
		return ProcessorProxyFactory.doExecute(strategyContext, this.iProcessor, ProcessorMethodEnum.executeImport);
	}
	
	@Override
	protected void afterImport(StrategyContext strategyContext) {
		try {
			if (strategyContext != null && strategyContext.getFileStrategySetting().isSaveLog()) {
				if (strategyContext.getImportLogObjects() != null && strategyContext.getImportLogObjects().size() > 0) {
					ProcessorProxyFactory.doExecute(strategyContext, this.iProcessor, ProcessorMethodEnum.saveOperateLogMulti);
				}
			}
		} catch (Exception e) {
			this.logger.error(String.format("BaseImportFileExecutor#afterImport saveOperateLog ERROR param【%s】 :", 
					JSON.toJSONString(strategyContext)), e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean saveOperateLogMulti(List<?> executeData) {
		return super.saveOperateLog((List<ImportLogObject>) executeData);
	}
}
